package org.example.everyday;

import javax.sound.midi.Soundbank;
import java.util.ArrayDeque;
import java.util.Deque;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * <p>
 *
 * @author anlu
 * @date 2022/7/13 15:33
 */
public class Lc735 {
    public static void main(String[] args) {
        Lc735 lc735 = new Lc735();

        int[] asteroids = {5,10,-5};
        int[] res = lc735.asteroidCollision(asteroids);
        for(int i : res){
            System.out.println(i);
        }
    }

    public int[] asteroidCollision(int[] asteroids) {
        Deque<Integer> stack = new ArrayDeque<Integer>();
        for(int aster : asteroids){
            boolean alive = true;
            while (alive && aster < 0 && !stack.isEmpty() && stack.peek() > 0){
                alive = stack.peek() < -aster;
                if(stack.peek() <= -aster){
                    stack.pop();
                }
            }
            if(alive){
                stack.push(aster);
            }
        }
        int size = stack.size();
        int[] ans = new int[size];
        for(int i = size - 1; i >= 0; i--){
            ans[i] = stack.pop();
        }
        return ans;
    }
}
